package com.zis.system.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zis.starter.core.Constant;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.system.dao.ResDao;
import com.zis.system.dao.SysDao;
import com.zis.system.inter.IResInter;
import com.zis.system.model.Res;
import com.zis.system.model.Sys;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by wangshen 2018/3/28
 */
@Service
@Transactional("txManager")
public class ResInterImpl implements IResInter {

    @Autowired
    private ResDao resDao;

    @Autowired
    private SysDao sysDao;

    @Override
    public ResponseEntity insert(Res model) throws Exception {
        int num = resDao.getSQLManager().insert(model);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity update(Res model) throws Exception {
        int num = resDao.getSQLManager().updateTemplateById(model);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity delete(String id) throws Exception {
        int num = resDao.getSQLManager().deleteById(Res.class, id);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity selectById(String id) throws Exception {
        Res result = resDao.unique(id);
        if (result != null) {
            return ResponseEntity.success(JSON.parseObject(JSON.toJSONString(result)));
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    @Override
    public PageQuery<Res> findByPage(SearchParams params) throws Exception {
        PageQuery<Res> query = new PageQuery<>();
        query.setPageNumber(params.getPage());
        query.setPageSize(params.getPageSize());
        query.setParas(params.getParams());
        resDao.getSQLManager().pageQuery("system.res.query", Res.class, query);
        return query;
    }

    @Override
    public ResponseEntity findByCondition(Map params) throws Exception {
        List<Res> resultList = resDao.getSQLManager().select("system.res.query", Res.class, params);
        if (resultList != null && resultList.size() > 0) {
            return ResponseEntity.success(JSON.parseArray(JSON.toJSONString(resultList)));
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    /**
     * 查询全部菜单转为tree
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity findAllConvertTree() throws Exception {
        List<Sys> sysList = sysDao.getSQLManager().select("system.sys.query", Sys.class, null);
        if(sysList != null && sysList.size() > 0) {
            JSONArray rootArray = new JSONArray();
            //循环建立根节点
            for (Sys sys : sysList) {
                LinkedHashMap<String, Object> rootNode = new LinkedHashMap<>();
                rootNode.put("id", sys.getSysId());
                rootNode.put("pid", "0");
                rootNode.put("name", sys.getSysName());
                rootNode.put("idKey", sys.getSysUrl());
                rootNode.put("open", true);
                rootNode.put("nocheck", false);
                //获取一级菜单节点信息
                List<Res> rootResList = resDao.querySubRes(sys.getSysId(), sys.getSysId());
                if (rootResList != null && rootResList.size() > 0) {
                    JSONArray rootResArray = new JSONArray();
                    for (Res rootRes : rootResList) {
                        LinkedHashMap<String, Object> rootResNode = new LinkedHashMap<>();
                        rootResNode.put("id", rootRes.getResId());
                        rootResNode.put("pid", sys.getSysId());
                        rootResNode.put("name", rootRes.getResName());
                        rootResNode.put("idKey", rootRes.getResUrl());
                        rootResNode.put("chkDisabled", false);
                        rootResNode.put("checked", false);
                        //处理二级菜单
                        List<Res> res2NodeList = resDao.querySubRes(rootRes.getResId(), null);
                        if (res2NodeList != null && res2NodeList.size() > 0) {
                            rootResNode.put("open", true);//存在下级打开菜单
                            JSONArray res2NodeArray = new JSONArray();
                            for (Res res2Menu : res2NodeList) {
                                LinkedHashMap<String, Object> res2Node = new LinkedHashMap<>();
                                res2Node.put("id", res2Menu.getResId());
                                res2Node.put("pid", rootRes.getResId());
                                res2Node.put("name", res2Menu.getResName());
                                res2Node.put("idKey", res2Menu.getResUrl());
                                res2Node.put("chkDisabled", false);
                                res2Node.put("checked", false);
                                //处理菜单资源
                                List<Res> menuResList = resDao.querySubRes(res2Menu.getResId(), null);
                                if (menuResList != null && menuResList.size() > 0) {
                                    res2Node.put("open", true);//存在下级打开菜单
                                    JSONArray menuResNodeArray = new JSONArray();
                                    for (Res menuRes : menuResList) {
                                        LinkedHashMap<String, Object> menuResNode = new LinkedHashMap<>();
                                        menuResNode.put("id", menuRes.getResId());
                                        menuResNode.put("pid", res2Menu.getResId());
                                        menuResNode.put("name", menuRes.getResName());
                                        menuResNode.put("idKey", menuRes.getResUrl());
                                        menuResNode.put("chkDisabled", false);
                                        menuResNode.put("checked", false);
                                        menuResNodeArray.add(menuResNode);
                                    }
                                    res2Node.put("children", menuResNodeArray);
                                }
                                res2NodeArray.add(res2Node);
                            }
                            rootResNode.put("children",  res2NodeArray);
                        }
                        rootResArray.add(rootResNode);
                    }
                    rootNode.put("children", rootResArray);
                }
                rootArray.add(rootNode);
            }
            return ResponseEntity.success(rootArray);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }
}
